package com.example.xmppclient;

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Presence;

import android.app.Dialog;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class SettingsDialog extends Dialog implements android.view.View.OnClickListener{
	private MainActivity xmppClient;
	
	public SettingsDialog(MainActivity xmppClient){
		super(xmppClient);
		this.xmppClient = xmppClient;
	}
	
	protected void onStart(){
		super.onStart();
		setContentView(R.layout.settings);
		getWindow().setFlags(4, 4);
		setTitle("XMPP Settings");
		Button ok = (Button) findViewById(R.id.ok);
		ok.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		String host = "talk.google.com";//getText(R.id.host);
		String port = "5222";//getText(R.id.port);
		String service = "gmail.com";//getText(R.id.service);
		String username = getText(R.id.userid);
		String password = getText(R.id.password);
		
		//Create a connection
		ConnectionConfiguration connConfig = new ConnectionConfiguration("talk.google.com", 5222, "gmail.com");
		XMPPConnection connection = new XMPPConnection(connConfig);
		
		try{
			connection.connect();
			Log.i("XMPPClient","[SettingsDialog] Connected to "+connection.getHost());
		}catch(XMPPException ex){
			Log.e("XMPPClient","[SettingsDialog] Failed to connect to "+connection.getHost());
			System.out.println(ex.getMessage());
			xmppClient.setConnection(null);
		}
		try{
			connection.login(username,password);
			Log.i("XMPPClient","Logged in as "+connection.getUser());
			
			// Set the status to available
			Presence presence = new Presence(Presence.Type.available);
			connection.sendPacket(presence);
			xmppClient.setConnection(connection);
		}catch(XMPPException ex){
			Log.e("XMPPClient", "[SettingsDialog] Failed to log in as "+username);
			xmppClient.setConnection(null);
		}
		dismiss();
	}
	
	private String getText(int id){
		EditText widget = (EditText) this.findViewById(id);
		return widget.getText().toString();
	}
}
